package com.chuangfeng.building.ms.menu.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.chuangfeng.building.common.CoreDaoImpl;
import com.chuangfeng.building.entity.TMsMenuInfo;
import com.chuangfeng.building.ms.menu.dao.MsMenuDao;
import com.chuangfeng.building.ms.menu.vo.TPCMenu;
import com.chuangfeng.framework.core.common.config.Constants;
import com.chuangfeng.framework.core.exception.PersistenceException;

/**
 * 
 * @author wangbing
 * 实现了对数据库中，后台菜单表的操作方法。
 */
@Repository
public class MsMenuDaoImpl extends CoreDaoImpl<TMsMenuInfo> implements MsMenuDao {
	
	/**
	 * 得到某角色下的菜单列表
	 */
	public List<TPCMenu> getMenuListByRoleId(Integer roleId) {
		try{
			List<TPCMenu>  allMenu = new ArrayList<TPCMenu>();
			List<TMsMenuInfo> parList = this.getParMeunListByRoleId(roleId);
			for(TMsMenuInfo parMenu:parList){
				TPCMenu childMenu = new TPCMenu();
				Integer menuId = parMenu.getMsMenuId();
				List<TPCMenu> childList = this.getChildMenuListByParent(menuId);
				if(childList.size()==0){
					childMenu.setIsHaveChild("false");
				}
				childMenu.setList(childList);
				childMenu.setMsMenuInfo(parMenu);
				allMenu.add(childMenu);
			}
			return allMenu;
		} catch (Exception e) {
			logger.error("得到某角色下的菜单列表,异常信息为: " + e.getMessage());
			throw new PersistenceException("类: "+this.getClass()+ "的 getMenuListByRoleId(Integer roleId) 得到某角色下的菜单列表时出现异常："+e.getMessage());
		}
	}

	/**
	 * 得到某角色下的一级菜单
	 * @param roleId
	 * @return
	 */
	public List<TMsMenuInfo> getParMeunListByRoleId(Integer roleId){
		try{
			String parHql = "select menu from TMsMenuInfo menu where menu.parent is null " +
					        " and menu.msMenuId in(select rolemenu.msMenuId  from TMsRoleMenu rolemenu where rolemenu.msRoleId = "+roleId+")";
			return this.find(parHql, 0, Constants.SELECT_MORE_SIZE);
		} catch (Exception e) {
			logger.error("得到某角色下的一级菜单列表,异常信息为: " + e.getMessage());
			throw new PersistenceException("类: "+this.getClass()+ "的 getParMeunListByRoleId(Integer roleId) 得到某角色下的菜单列表时出现异常："+e.getMessage());
		}
	}
	
	/**
	 * 得到某一菜单下的所有子节点
	 * @param parentId
	 * @return
	 */
	public List<TPCMenu> getChildMenuListByParent(Integer parentId){
		try{ 
			List<TPCMenu> resultList = new ArrayList<TPCMenu>();
			String parHql = "select menu1 from TMsMenuInfo menu1 where menu1.msMenuId in " +
					        " (select menu2.msMenuId  from TMsMenuInfo menu2 where menu2.parent = "+parentId+")";
			List<TMsMenuInfo> list =  this.find(parHql, 0, Constants.SELECT_MORE_SIZE);
			for(TMsMenuInfo one:list){
				TPCMenu menu = new TPCMenu();
				Integer menuId = one.getMsMenuId();
				List<TPCMenu> childList = this.getChildMenuListByParent(menuId);
				if(childList.size()==0){
					menu.setIsHaveChild("false");
				}
				menu.setList(childList);
				menu.setMsMenuInfo(one);
				resultList.add(menu);
			}
			return resultList;
		} catch (Exception e) {
			logger.error("得到某一菜单下的所有子节点列表,异常信息为: " + e.getMessage());
			throw new PersistenceException("类: "+this.getClass()+ "的 getChildMenuListByParent(Integer parentId) 得到某一菜单下的所有子节点时出现异常："+e.getMessage());
	   }	
	}
}
